<?php

/**
 * narudzbina actions.
 *
 * @package    marketing
 * @subpackage narudzbina
 * @author     Your name here
 */
class narudzbinaActions extends sfActions {

    public function executeIndex(sfWebRequest $request) {
        $date= getdate();
	$default=array("month"=>$date["mon"] , "day"=>$date["mday"],"year"=>$date["year"] );
        
        $date=getdate(mktime(0,0,0, $date["mon"]-1, $date["mday"], $date["year"]));
        $default1=array("month"=>$date["mon"] , "day"=>$date["mday"],"year"=>$date["year"] );
        $filter = array(
            "od" => $default1,
            "do" => $default,
	    "status" => '',
        );
        if ($request->hasParameter('id')) {
            $c = new Criteria();
            $c->add(NarudzbinaPeer::ID, $request->getParameter('id'));
        } else {
            if ($request->hasParameter('narudzbina')) {


                $this->getUser()->setAttribute('narudzbina', $request->getParameter('narudzbina'));
                $this->redirect('narudzbina/index');
            }
            if ($this->getUser()->hasAttribute('narudzbina')) {
                $filter = $this->getUser()->getAttribute('narudzbina');
            }
            $c1 = null;
            $c2 = null;
            $c = new Criteria();
            if ($filter['od'] != '') {
                $from = date('Y-m-d', mktime(0, 0, 0, $filter['od']['month'], $filter['od']['day'], $filter['od']['year']));
                $c1 = $c->getNewCriterion(NarudzbinaPeer::DATUM_NARUDZBINE, $from, Criteria::GREATER_EQUAL);
            }
            if ($filter['do'] != '') {
                $from = date('Y-m-d', mktime(0, 0, 0, $filter['do']['month'], $filter['do']['day'], $filter['do']['year']));
                $c2 = $c->getNewCriterion(NarudzbinaPeer::DATUM_NARUDZBINE, $from, Criteria::LESS_EQUAL);
            }

            if ($c1 && $c2) {
                $c1->addAnd($c2);
                $c->add($c1);
            } else if ($c1)
                $c->add($c1);
            else if ($c2)
                $c->add($c2);
            if (!is_null($filter['status']) && $filter['status'] != null) {
                $c->add(NarudzbinaPeer::STATUS_ID, $filter['status']);
            }
        }
        $this->filter = new NarudzbinaFilterForm();
        $this->filter->setDefaults($filter);
          $this->pager=new sfPropelPager('Narudzbina', 3);
            $this->pager->setMaxPerPage(3);
            $this->pager->setCriteria($c);
            $this->pager->setPeerMethod('doSelectJoinAll');
            $this->pager->setPage($request->hasParameter('page')?$request->getParameter('page'):1);
            $this->pager->init();
            $this->narudzbina_list = $this->pager->getResults();
        $narudzbinaIds = array();
        foreach ($this->narudzbina_list as $narudzbina) {
            $narudzbinaIds[] = $narudzbina->getId();
        }
        $c = new Criteria();
        $c->add(NarudzbinaProzvodPeer::NARUDZBINA_ID, $narudzbinaIds, Criteria::IN);
        $proizvods = NarudzbinaProzvodPeer::doSelectJoinAll($c);
        $list = array();
        foreach ($proizvods as $proizvod) {
            $entry = array();
            $entry['naziv'] = $proizvod->getProizvod()->getNaziv();
            $entry['kolicina'] = $proizvod->getKolicina();
            $list[$proizvod->getNarudzbinaId()][] = $entry;
        }
        $this->list = $list;
    }



}
